package com.bestom.bestoma10demo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.util.Log;

import androidx.annotation.RequiresApi;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;

/**
  * Broadcast receiver that monitors the status of network connections
  * @author bestom
  *
  */
public class NetworkStateReceiver extends BroadcastReceiver {

    @Override
     public void onReceive(Context context, Intent intent) {

         Log.i("debug",intent.getAction());

        //1. Gets the network connection management component of the system
        ConnectivityManager connMgr=
                 (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        //2. Gets the current network connection object
         NetworkInfo activeNetworkInfo = connMgr.getActiveNetworkInfo();

         if(activeNetworkInfo!=null){
             int type = activeNetworkInfo.getType();
             String detailedState = activeNetworkInfo.getDetailedState().toString();
             String extraInfo = activeNetworkInfo.getExtraInfo();
             String typeName = activeNetworkInfo.getTypeName();
             String reason = activeNetworkInfo.getReason();
             String subtypeName = activeNetworkInfo.getSubtypeName();
             int subtype = activeNetworkInfo.getSubtype();
             Log.i("debug", "net type --->" + type);
             Log.i("debug", "net extraInfo --->" + extraInfo);
             Log.i("debug", "net typeName --->" + typeName);
             Log.i("debug", "net reason --->" + reason);
             Log.i("debug", "net subtypeName --->" + subtypeName);
             Log.i("debug", "net subtype --->" + subtype);
             Log.i("debug", "net detailedState --->" + detailedState);

             for (NetworkInterface networkInterface : getNetworkInterface()){
                 String interfaceName = networkInterface.getDisplayName();
                 String string = networkInterface.toString();
                 Log.i("debug", "interfaceName:" + interfaceName);

                 Enumeration<InetAddress> enumIp = networkInterface
                         .getInetAddresses();
                 while (enumIp.hasMoreElements()) {
                     // Returns information about the next IP address in the enumeration collection
                     InetAddress inetAddress = enumIp.nextElement();
                     Log.i("debug", "address:" + inetAddress.getHostAddress());
                 }
             }
         }
     }

    private ArrayList<NetworkInterface> getNetworkInterface(){
        ArrayList<NetworkInterface> mList = new ArrayList<>();
        // 获取本地设备的所有网络接口
        Enumeration<NetworkInterface> enumerationNi;
        try {
            enumerationNi = NetworkInterface.getNetworkInterfaces();
            while (enumerationNi.hasMoreElements()){
                mList.add(enumerationNi.nextElement());
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return mList;
    }

    public static String getNetStatus(Context context) {
        ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connManager == null) {
            return null;
        }
        NetworkInfo activeNetInfo = connManager.getActiveNetworkInfo();
        if (activeNetInfo == null || !activeNetInfo.isAvailable()) {
            return null;
        }

        // Check if it's connected wifi
        NetworkInfo wifiInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (null != wifiInfo) {

            NetworkInfo.State state = wifiInfo.getState();
            if (null != state) {
                if (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING) {
                    return "wifi";
                }
            }
        }

        // Check if it's connected vpn
        NetworkInfo vpnInfo = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            vpnInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_VPN);
        }
        if (null != vpnInfo) {

            NetworkInfo.State state = vpnInfo.getState();
            if (null != state) {
                if (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING) {
                    return "vpn";
                }
            }
        }

        // Check if it's connected ethernet
        NetworkInfo ethernetInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
        if (null != ethernetInfo) {
            NetworkInfo.State state = ethernetInfo.getState();
            if (null != state) {
                if (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING) {
                    return "ethernet";
                }
            }
        }

        // If it is not WIFI, then judge what kind of network is currently connected to the operator 2G, 3G, 4G, etc
        NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (null != networkInfo) {
            NetworkInfo.State state = networkInfo.getState();
            String strSubTypeName = networkInfo.getSubtypeName();
            if (null != state) {
                if (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING) {
                    switch (activeNetInfo.getSubtype()) {
                        // If it's 2G type
                        case TelephonyManager.NETWORK_TYPE_GPRS:
                        case TelephonyManager.NETWORK_TYPE_CDMA:
                        case TelephonyManager.NETWORK_TYPE_EDGE:
                        case TelephonyManager.NETWORK_TYPE_1xRTT:
                        case TelephonyManager.NETWORK_TYPE_IDEN:
                            return "2G";
                        // If it's 3G type
                        case TelephonyManager.NETWORK_TYPE_EVDO_A:
                        case TelephonyManager.NETWORK_TYPE_UMTS:
                        case TelephonyManager.NETWORK_TYPE_EVDO_0:
                        case TelephonyManager.NETWORK_TYPE_HSDPA:
                        case TelephonyManager.NETWORK_TYPE_HSUPA:
                        case TelephonyManager.NETWORK_TYPE_HSPA:
                        case TelephonyManager.NETWORK_TYPE_EVDO_B:
                        case TelephonyManager.NETWORK_TYPE_EHRPD:
                        case TelephonyManager.NETWORK_TYPE_HSPAP:
                            return "3G";
                        // If it's 4G type
                        case TelephonyManager.NETWORK_TYPE_LTE:
                            return "4G";
                        default:
                            if (strSubTypeName.equalsIgnoreCase("TD-SCDMA") || strSubTypeName.equalsIgnoreCase("WCDMA") || strSubTypeName.equalsIgnoreCase("CDMA2000")) {
                                return "3G";
                            } else {
                                return strSubTypeName;
                            }
                    }
                }
            }
        }
        return null;
    }
 }

